package com.android.gufengmall.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.GoodsBean;
import com.android.gufengmall.bean.VideoBean;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 视频列表数据适配器
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.VideoViewHolder> {

    private final static String TAG = "VideoAdapter";

    private Context mContext;
    private List<VideoBean> mVideoBeans;
    private List<GoodsBean> mGoodsBeans;
    private boolean mIsVideo;
    private VideoListener mListener;

    public SearchAdapter(Context context,boolean isVideo, List<VideoBean> videoBeans,List<GoodsBean> goodsBeans) {
        mContext = context;
        mIsVideo = isVideo;
        mVideoBeans = videoBeans;
        mGoodsBeans = goodsBeans;
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VideoViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_search_video,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        if (mIsVideo) {
            VideoBean videoBean = mVideoBeans.get(position);
            Glide.with(mContext).load(BitmapFactory.decodeFile(videoBean.getVideoImg()))
                    .dontAnimate().error(R.drawable.icon_error_qqsh).centerCrop().into(holder.mImgVideo);
            holder.mVideoTitle.setText(videoBean.getVideoTitle());
            holder.itemView.setOnClickListener(v -> {
                mListener.onSearchVideoClick(videoBean.getVideoTitle());
            });
        } else {
            GoodsBean goodsBean = mGoodsBeans.get(position);
            Glide.with(mContext).load(BitmapFactory.decodeFile(goodsBean.getGoods_imgShop()))
                    .dontAnimate().error(R.drawable.icon_shop_6).centerCrop().into(holder.mImgVideo);
            holder.mVideoTitle.setText(goodsBean.getGoods_name());
            holder.itemView.setOnClickListener(v -> {
                mListener.onSearchVideoClick(goodsBean.getGoods_name());
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mIsVideo) {
            return mVideoBeans.size();
        } else {
            return mGoodsBeans.size();
        }
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {

        private final ImageView mImgVideo;
        private final TextView mVideoTitle;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);

            mImgVideo = itemView.findViewById(R.id.img_video);
            mVideoTitle = itemView.findViewById(R.id.tv_video_title);


        }
    }

    public void setOnVideoListener(VideoListener videoListener) {
        mListener = videoListener;
    }

    public interface VideoListener {
        void onSearchVideoClick(String goodsName);
    }
}
